<#assign hasPrimaryKey = false />
<#list table.columnList as column>
	<#if column.primaryKey>
		<#assign hasPrimaryKey = true>
		<#break>
	</#if>
</#list>
package ${package};

import java.util.ArrayList;
import java.util.List;

import org.sosostudio.dbunifier.DbUnifier;
import org.sosostudio.dbunifier.Row;
import org.sosostudio.dbunifier.RowSet;
import org.sosostudio.dbunifier.autocode.PaginationArrayList;
import org.sosostudio.dbunifier.oom.ConditionClause;
import org.sosostudio.dbunifier.oom.DeleteSql;
import org.sosostudio.dbunifier.oom.ExtraClause;
import org.sosostudio.dbunifier.oom.InsertKeyValueClause;
import org.sosostudio.dbunifier.oom.InsertSql;
import org.sosostudio.dbunifier.oom.LogicalOp;
import org.sosostudio.dbunifier.oom.OrderByClause;
import org.sosostudio.dbunifier.oom.RelationOp;
import org.sosostudio.dbunifier.oom.SelectSql;
import org.sosostudio.dbunifier.oom.UpdateKeyValueClause;
import org.sosostudio.dbunifier.oom.UpdateSql;

/**
 * ${table.name}
 *
 * @author generated by db-unifier autocode
 */
public class ${table.definationName}Dao {

	private DbUnifier unifier;

	public ${table.definationName}Dao() {
		unifier = new DbUnifier();
	}
	
	public ${table.definationName}Dao(DbUnifier unifier) {
		this.unifier = unifier;
	}

	<#if table.table>
	public int add${table.definationName}(${table.definationName} ${table.variableName}) {
		${table.variableName}.validateString(unifier.getEncoding());
		InsertSql insertSql = new InsertSql().setTableName(${table.definationName}.${table.name})
			.setInsertKeyValueClause(
				new InsertKeyValueClause()
		<#list table.columnList as column>
					.add${column.type.getName()}Clause(${table.definationName}.${column.name}, ${table.variableName}.get${column.definationName}())
		</#list>
		);
		return unifier.executeInsertSql(insertSql);
	}

	<#if hasPrimaryKey>
	public int update${table.definationName}(${table.definationName} ${table.variableName}) {
		${table.variableName}.validateString(unifier.getEncoding());
		UpdateSql updateSql = new UpdateSql().setTableName(${table.definationName}.${table.name})
			.setUpdateKeyValueClause(
				new UpdateKeyValueClause()
		<#list table.columnList as column>
					.add${column.type.getName()}Clause(${table.definationName}.${column.name}, ${table.variableName}.get${column.definationName}())
		</#list>
			).setConditionClause(
				new ConditionClause(LogicalOp.AND)
		<#list table.columnList as column>
			<#if column.primaryKey>
				.add${column.type.getName()}Clause(${table.definationName}.${column.name}, RelationOp.EQUAL, ${table.variableName}.get${column.definationName}())
			</#if>
		</#list>	
			);
		return unifier.executeUpdateSql(updateSql);
	}
	</#if>

	public int update${table.definationName}(UpdateKeyValueClause updateKeyValueClause, ConditionClause conditionClause) {
		UpdateSql updateSql = new UpdateSql().setTableName(${table.definationName}.${table.name})
			.setUpdateKeyValueClause(updateKeyValueClause)
			.setConditionClause(conditionClause);
		return unifier.executeUpdateSql(updateSql);
	}
	
	public int delete${table.definationName}(ConditionClause conditionClause) {
		DeleteSql deleteSql = new DeleteSql().setTableName(${table.definationName}.${table.name})
			.setConditionClause(conditionClause);
		return unifier.executeDeleteSql(deleteSql);
	}
	</#if>

	public PaginationArrayList<${table.definationName}> query${table.definationName}(ConditionClause conditionClause, ExtraClause extraClause, OrderByClause orderByClause, int pageSize, int pageNumber) {
		SelectSql selectSql = new SelectSql().setTableName(${table.definationName}.${table.name})
			.setColumns("*")
			.setConditionClause(conditionClause)
			.setExtraClause(extraClause)
			.setOrderByClause(orderByClause);
		RowSet rowSet = unifier.executeSelectSql(selectSql, pageSize, pageNumber);
		PaginationArrayList<${table.definationName}> pal = new PaginationArrayList<${table.definationName}>(rowSet.getPageSize(), rowSet.getPageNumber(), rowSet.getTotalRowCount());
		for (int i = 0; i < rowSet.size(); i++) {
			Row row = rowSet.getRow(i);
			${table.definationName} ${table.variableName} = new ${table.definationName}();
			<#list table.columnList as column>
			${table.variableName}.set${column.definationName}(row.get${column.type.getName()}(${table.definationName}.${column.name}));
			</#list>
			pal.add(${table.variableName});
		}
		return pal;
	}

	public List<${table.definationName}> query${table.definationName}(ConditionClause conditionClause, ExtraClause extraClause, OrderByClause orderByClause) {
		SelectSql selectSql = new SelectSql().setTableName(${table.definationName}.${table.name})
			.setColumns("*")
			.setConditionClause(conditionClause)
			.setExtraClause(extraClause)
			.setOrderByClause(orderByClause);
		RowSet rowSet = unifier.executeSelectSql(selectSql);
		ArrayList<${table.definationName}> al = new ArrayList<${table.definationName}>();
		for (int i = 0; i < rowSet.size(); i++) {
			Row row = rowSet.getRow(i);
			${table.definationName} ${table.variableName} = new ${table.definationName}();
			<#list table.columnList as column>
			${table.variableName}.set${column.definationName}(row.get${column.type.getName()}(${table.definationName}.${column.name}));
			</#list>
			al.add(${table.variableName});
		}
		return al;
	}

}